//
//  XNewVersionAlertView.m
//  ReceiveVouchers
//
//  Created by admin on 2017/7/6.
//  Copyright © 2017年 LiX i n long. All rights reserved.
//

#import "XPersonDataAlertView.h"

#import "UIImage+VR.h"


@interface XPersonDataAlertView()


//内容
@property (nonatomic,retain) UILabel *msgLbl;
//横线线
@property (nonatomic,retain) UIView *lineView;
//竖线
@property (nonatomic,retain) UIView *vLineView;
// 背景图
@property (nonatomic, strong) UIImageView *backgrounImageView;


@end

@implementation XPersonDataAlertView

- (instancetype)initWithTitle:(NSString *)title sureBtnTitle:(NSString *)sureTitle cancleBtnTitle:(NSString *)cancleTitle size:(CGSize)size
{
    if (self = [super init]) {
        
        self.isHidenAlertBySure = YES;
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = RGBA(0, 0, 0, 0.5);
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 10.0;
        self.alertView.layer.masksToBounds = YES;
        
        self.alertView.frame = CGRectMake(0, 0, size.width, size.height);
        self.alertView.center = self.center;
        self.alertView.centerY = self.centerY - 25;
//        self.alertView.layer.position = self.center;
        
        if (title) {
            
            self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, size.width, 15)];
            self.titleLbl.numberOfLines = 0;
            self.titleLbl.text = title;
            self.titleLbl.textColor = RGB(48, 48, 48);
            self.titleLbl.textAlignment = NSTextAlignmentCenter;
            self.titleLbl.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
            self.titleLbl.frame = CGRectMake(0, 40, size.width, 15);
            [self.alertView addSubview:self.titleLbl];
        }
        
        // 确定按钮
        self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.sureBtn.tag = 101;
//        [self.sureBtn setBackgroundImage:[self imageWithColor:RGB(255, 255, 255)] forState:UIControlStateNormal];
        [self.sureBtn setBackgroundImage:[UIImage imageWithColor:RGBA(0, 0, 0, 0.1)] forState:UIControlStateHighlighted];
        [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
        [self.sureBtn setTitleColor:RGB(64, 64, 64) forState:UIControlStateNormal];
        [self.sureBtn setTitleColor:RGB(200, 199, 204) forState:UIControlStateDisabled];
        self.sureBtn.titleLabel.font = VRFont(15);
        [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.sureBtn];
        
        // 取消按钮
        self.cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancleBtn.tag = 100;
//        [self.cancleBtn setBackgroundImage:[self imageWithColor:RGB(255, 255, 255)] forState:UIControlStateNormal];
        [self.cancleBtn setBackgroundImage:[UIImage imageWithColor:RGBA(0, 0, 0, 0.1)] forState:UIControlStateHighlighted];
        [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
        [self.cancleBtn setTitleColor:RGB(64, 64, 64) forState:UIControlStateNormal];
        [self.cancleBtn setTitleColor:RGB(200, 199, 204) forState:UIControlStateDisabled];
        self.cancleBtn.titleLabel.font = VRFont(15);
        [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.cancleBtn];
        
        // 横线
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = RGB(234, 234, 234);
        self.lineView.frame = CGRectMake(10, size.height - 44, size.width - 20, 1);
        [self.alertView addSubview:self.lineView];
        
        // 竖线
        self.vLineView = [[UIView alloc] init];
        self.vLineView.frame = CGRectMake(size.width * 0.5 - 0.5, size.height - 25 - 9, 1, 25);
        self.vLineView.backgroundColor = RGB(234, 234, 234);
        [self.alertView addSubview:self.vLineView];
        
        if (cancleTitle && sureTitle) {
            
            self.cancleBtn.frame = CGRectMake(0, size.height - 44, size.width * 0.5, 44);
            self.sureBtn.frame = CGRectMake(size.width * 0.5, size.height - 44, size.width * 0.5, 44);
        }else if (sureTitle){
            
            self.cancleBtn.hidden = YES;
            self.vLineView.hidden = YES;
            self.sureBtn.frame = CGRectMake(0, size.height - 44, size.width, 44);
        }else if(cancleTitle){
            
            self.sureBtn.hidden = YES;
            self.vLineView.hidden = YES;
            self.cancleBtn.frame = CGRectMake(0, size.height - 44, size.width, 44);
        }
        
        [self addSubview:self.alertView];
    }
    
    return self;
}


#pragma mark - 弹出 -
- (void)show
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [rootWindow bringSubviewToFront:self];
    [self creatShowAnimation];
}

- (void)dissmiss
{
    [self removeFromSuperview];
}

- (void)creatShowAnimation
{
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}

// 弹性动画
- (void)showOscillatoryAnimationWithLayer:(CALayer *)layer type:(BOOL)type{
    NSNumber *animationScale1 = type ? @(1.15) : @(0.5);
    NSNumber *animationScale2 = type ? @(0.92) : @(1.15);
    
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [layer setValue:animationScale1 forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [layer setValue:animationScale2 forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                [layer setValue:@(1.0) forKeyPath:@"transform.scale"];
            } completion:nil];
        }];
    }];
}


#pragma mark - action
- (void)buttonEvent:(UIButton *)sender
{
    if (self.ActionBlock) {
        self.ActionBlock(sender.tag - 100);
    }

    if (sender.tag == 100 || self.isHidenAlertBySure) {
        [self dissmiss];
    }
}

@end
